namespace DynamicShadows
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;

    /// <summary>
    /// A source of light that should be rendered into the light map
    /// </summary>
    class LightSource
    {
        #region Fields

        private Color color;
        private Texture2D lightTexture;
        private Vector2 position;
        private float range;
        private float rotation;

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Constructs a new instance of LightSource.
        /// </summary>
        /// <param name="texture">The texture of the light</param>
        /// <param name="color">The color of the light</param>
        /// <param name="range">The range within which the light will illuninate objects</param>
        /// <param name="position">The position of the lightsource</param>
        public LightSource(Texture2D texture, Color color, float range, Vector2 position)
        {
            lightTexture = texture;
            this.color = color;
            this.range = range;
            this.position = position;
        }

        #endregion Constructors

        #region Properties

        /// <summary>
        /// The color of the light
        /// </summary>
        public Color Color
        {
            get { return color; }
            set { color = value; }
        }

        /// <summary>
        /// The texture to apply to the light
        /// </summary>
        public Texture2D LightTexture
        {
            get { return lightTexture; }
            set { lightTexture = value; }
        }

        /// <summary>
        /// The position of the lightsource
        /// </summary>
        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        /// <summary>
        /// The range that the light should spread
        /// </summary>
        public float Range
        {
            get { return range; }
            set { range = value; }
        }

        /// <summary>
        /// The rotation of the lightsource
        /// </summary>
        public float Rotation
        {
            get { return rotation; }
            set { rotation = value; }
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Draws the light source onto the passed in sprite batch.
        /// </summary>
        /// <param name="spriteBatch">The spritebatch to draw the light onto.</param>
        public void Draw(SpriteBatch spriteBatch)
        {
            Vector2 center = new Vector2(lightTexture.Width / 2, lightTexture.Height / 2);
            float scale = range / ((float)lightTexture.Width / 2.0f);
            spriteBatch.Draw(lightTexture, position, null, color, 0, center, scale,SpriteEffects.None,1);
        }

        /// <summary>
        /// Draws a rotated light source taking the rotation of the source in respect. The light source is then Drawn onto the spritebatch.
        /// </summary>
        /// <param name="spriteBatch">The spritebatch to draw the source onto</param>
        public void DrawRotated(SpriteBatch spriteBatch)
        {
            Vector2 center = new Vector2(lightTexture.Width / 2, lightTexture.Height);
            float scale = range / ((float)lightTexture.Width / 2.0f);
            spriteBatch.Draw(lightTexture, position, null, color, rotation + MathHelper.ToRadians(90), center, scale, SpriteEffects.None, 1);
        }

        #endregion Methods
    }
}